package com.toubutu.audio.admin.controller;

import com.toubutu.anno.LoginRequired;
import com.toubutu.audio.admin.dto.AdminVideoQueryDto;
import com.toubutu.audio.admin.dto.MergeFileDto;
import com.toubutu.audio.admin.dto.UploadFileDto;
import com.toubutu.audio.admin.dto.VideoDto;
import com.toubutu.audio.common.PermissionCode;
import com.toubutu.audio.dto.CheckFileExistDto;
import com.toubutu.audio.valid.ValidGroup;
import com.toubutu.audio.vo.VideoVo;
import com.toubutu.audio.service.VideoService;
import com.toubutu.common.PageUtils;
import com.toubutu.common.Result;
import com.toubutu.permission.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;

/**
 * @author 戒躁
 * @date 2024/1/8
 */
@RestController
@Api(tags = "后台接口--视频模块接口")
@RequestMapping("/admin/video")
@LoginRequired
public class AdminVideoController {

    @Resource
    private VideoService videoService;


    @PostMapping("/page/list")
    @ApiOperation("分页查询")
    @Permission(value = PermissionCode.Video.VIDEO_LIST)
    public Result<PageUtils<VideoVo>> list(@RequestBody AdminVideoQueryDto dto){
        return Result.ok(videoService.pageQueryAdminVoList(dto));
    }
    @PostMapping("/upload")
    @ApiOperation("视频上传")
    @Permission(value = PermissionCode.Video.VIDEO_UPLOAD)
    public Result<String> uploadVideo(@RequestParam("file") MultipartFile file,
                                      @RequestParam("md5")String md5,@RequestParam("seq")Integer seq,
                                      @RequestParam("totalSlices")Integer totalSlices,
                                      @RequestParam("videoCode") String videoCode
                                      ) throws IOException {
        UploadFileDto uploadFileDto = UploadFileDto.builder().md5(md5).seq(seq)
                .videoCode(videoCode)
                .totalSlices(totalSlices).file(file).build();
        return Result.ok(videoService.uploadVideo(uploadFileDto));
    }

    @PostMapping("/merge")
    @ApiOperation("文件合并请求")
    public Result<Void> mergeFile(@RequestBody @Valid MergeFileDto mergeFileDto){
        videoService.mergeVideoFile(mergeFileDto);
        return Result.ok();
    }

    @GetMapping("/info/{code}")
    @ApiOperation("根据视频编码获取视频信息")
    @Permission(value = PermissionCode.Video.VIDEO_DETAIL)
    public Result<VideoVo> getVideoInfoByCode(@PathVariable String code){
        return Result.ok(videoService.getVideoInfoByCode(code));
    }

    @PostMapping("/publish")
    @ApiOperation("发布视频")
    @Permission(value = PermissionCode.Video.VIDEO_ADD)
    public Result<Void> publishVideo(@RequestBody @Validated(value = {ValidGroup.Add.class}) VideoDto videoDto){
        videoService.publishVideo(videoDto);
        return Result.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改视频")
    @Permission(value = PermissionCode.Video.VIDEO_UPDATE)
    public Result<Void> updateVideo(@RequestBody @Validated(value = {ValidGroup.Update.class}) VideoDto videoDto){
        videoService.updateVideo(videoDto);
        return Result.ok();
    }

    @PostMapping("/exist")
    public Result<Boolean> videoSourceIsExist(@RequestBody CheckFileExistDto dto){
        return Result.ok(videoService.checkVideoSourceIsExist(dto));
    }
}
